﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;
using MySql.Data.MySqlClient;
using Dapper;

namespace DAL
{
    public class AlertDocDAL
    {
        public static async Task<IEnumerable<AlertDoc>> QueryAlertDocsAsync(AlertDocQueryCriteria criteria)
        {
            AlertDocQueryCriteria queryCrieria = criteria ?? new AlertDocQueryCriteria();
          
            string sql = @" SELECT A.id
                            , A.content 
                            , A.userIndustry 
                            , A.elementName  
                            , A.insertTime
                            , A.addMan 
                            , T.tagId 
                            , T.tagObjectName
                            FROM msap_db.alert_doc A
                            LEFT JOIN msap_db.tagobject_infor AS T 
                            ON T.tagId = A.tagObject
                            WHERE
                            (@TagObject IS NULL OR A.tagObject = @TagObject)
                            AND (@ElementName IS NULL OR A.elementName = @ElementName)
                            AND (@UserIndustry IS NULL OR A.userIndustry = @UserIndustry)
                            ORDER BY A.insertTime DESC";
            
            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return await conn.QueryAsync<AlertDoc, TagObjectInfo, AlertDoc>(sql,
                    (alertDoc, tagObjectInfo) =>
                    {
                        if (tagObjectInfo != null)
                        {
                            alertDoc.TagObjectInfo = tagObjectInfo;
                        }
                        return alertDoc;
                    },
                    param: new
                    {
                        TagObject = queryCrieria.TagObject,
                        ElementName = queryCrieria.ElementName,
                        UserIndustry = queryCrieria.UserIndustry
                    },
                    splitOn: "tagId");
            }
        }

        public static Task<int> InsertOrUpdateAlertDoc(AlertDoc alertDoc)
        {
            string sql = @"INSERT INTO  msap_db.alert_doc
                            (
                            ID
                            , content
                            , tagObject
                            , userIndustry
                            , elementName
                            , addMan
                            , insertTime
                            )
                            VALUES(@Id, @Content , @TagObject , @UserIndustry , @ElementName , @AddMan , @InsertTime)
                            ON DUPLICATE KEY UPDATE                            
                            content=VALUES(content)
                            ,tagObject=VALUES(tagObject)
                            ,userIndustry=VALUES(userIndustry)
                            ,elementName=VALUES(elementName)
                            ,addMan=VALUES(addMan)
                            ,insertTime=VALUES(insertTime)";
            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return conn.ExecuteAsync(sql, new {
                    Id = alertDoc.Id,
                    Content = alertDoc.Content,
                    TagObject = alertDoc.TagObjectInfo.TagId,
                    UserIndustry = alertDoc.UserIndustry,
                    ElementName = alertDoc.ElementName,
                    AddMan = alertDoc.AddMan,
                    InsertTime = alertDoc.InsertTime
                });
            }
        }

        public static Task<int> DeleteAlertDoc(List<int> alertDocIdList)
        {
            string sql = $"DELETE FROM msap_db.alert_doc WHERE ID IN ({string.Join(",", alertDocIdList)})";

            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return conn.ExecuteAsync(sql);
            }
        }
    }
}
